package be.dch.typeditor;

import java.io.File;

import be.dch.exceptions.InvalidParameterException;
import be.dch.typeditor.domain.GarminIcon;
import be.dch.typeditor.domain.Typ;
import be.dch.typeditor.parser.TypParser;

public class TypEditor {

    /** */
    public static final int POLYGLONS = 0;

    /** */
    public static final int POLYLINES = 1;

    /** */
    public static final int POIS = 2;

    private transient Typ typFile;
    private int selected;

    /**
     * @return point of interests (POIs) GarminIcon array
     */
    public GarminIcon[] getPois() {
	return this.typFile.getPois();
    }

    /**
     * @return Polyglons GarminIcon array
     */
    public GarminIcon[] getPolyglons() {
	return this.typFile.getPolyglons();
    }

    /**
     * @return Polylines GarminIcon array
     */
    public GarminIcon[] getPolylines() {
	return this.typFile.getPolylines();
    }

    /**
     * Method to get the size of the specified GarminIcon array.
     * 
     * @param type
     *            (see
     * @return
     * @throws InvalidParameterException
     */
    public int getSize(final int type) throws InvalidParameterException {
	int result;
	if (type == TypEditor.POLYGLONS) {
	    result = this.getPolyglons().length;
	} else if (type == TypEditor.POLYLINES) {
	    result = this.getPolylines().length;
	} else if (type == TypEditor.POIS) {
	    result = this.getPois().length;
	} else {
	    throw new InvalidParameterException(type + " is not supported");
	}
	return result;
    }

    public void saveFile(final File outputFile) {
	TypParser.toTyp(this.typFile, outputFile);
    }

    public void loadFile(final File inputFile) {
	this.newFile();
	TypParser.toTyp(this.typFile, inputFile);
    }

    public void newFile() {
	this.typFile = new Typ();
    }

    public void setSelected(final int selected) throws InvalidParameterException {
	if ((selected != TypEditor.POIS) && (selected != TypEditor.POLYGLONS) && (selected != TypEditor.POLYLINES)) {
	    throw new InvalidParameterException(selected + " is not supported");
	}
	this.selected = selected;
    }

    public int getSelected() {
	return this.selected;
    }

    public GarminIcon[] setSelectedAndGetGarminIconArray(final int selected) throws InvalidParameterException {
	GarminIcon[] result = new GarminIcon[0];
	if (selected != this.selected) {
	    this.setSelected(selected);
	}
	if (selected == TypEditor.POLYGLONS) {
	    result = this.getPolyglons();
	} else if (selected == TypEditor.POLYLINES) {
	    result = this.getPolylines();
	} else if (selected == TypEditor.POIS) {
	    result = this.getPois();
	}
	return result;
    }

    public GarminIcon[] getSelectedGarminIconArray() throws InvalidParameterException {
	return this.setSelectedAndGetGarminIconArray(this.selected);
    }

}
